<?php
	/**
	 * 将配置选项中的显示类型转换为html代码
	 */
	function format_config($data){
		foreach ($data as $k=>$v){
			$data[$k] = get_config_html($v);
		}
		return $data;
	}
	/**
	 * 获得显示类型的html代码
	 * @param unknown_type $v
	 */
	function get_config_html($v){
		$html = '';
		switch ($v['viewtype']){
			case 'string' :
				$html .= "<td class='input'><input type='text' name='{$v["name"]}' value='{$v["value"]}' /></td>";
				break;
			case 'select':
				$html .= "<td class='input'><select name='{$v["name"]}'>";
				$viewvalue = explode(',', $v['viewvalue']);
				foreach($viewvalue as $value){
					$s = explode('|', $value);
					if($s[0] == $v['value']){
						$html .= "<option value='".$s[0]."' selected='selected'>".$s['1']."</option>";
					}else{
						$html .= "<option value='".$s[0]."'>".$s['1']."</option>";
					}
				}
				$html .= "</select></td>";
				break;
			case 'radio':
				$html .= "<td class='input'>";
				$viewvalue = explode(',', $v['viewvalue']);
				foreach($viewvalue as $value){
					$s = explode('|', $value);
					if($s[0] == $v['value']){
						$html .= "<input type='radio' name='{$v["name"]}' value='{$s[0]}' checked='checked'/>{$s[1]}";
					}else{
						$html .= "<input type='radio' name='{$v["name"]}' value='{$s[0]}'/>{$s[1]}";
					}
				}
				$html .= "</td>";
				break;
			case 'checkbox':
				$html .= "<td class='input'>";
				$viewvalue = explode(',', $v['viewvalue']);
				foreach($viewvalue as $value){
					$s = explode('|', $value);
					if(strpos($v['value'],',')){
						$m = explode(',', $v['value']);
						if(in_array($s[0],$m)){
							$html .= "<input type='checkbox' name='{$v["name"]}[]' value='{$value[0]}' checked='checked'/>{$s[1]}";
						}else{
							$html .= "<input type='checkbox' name='{$v["name"]}[]' value='{$value[0]}'/>{$s[1]}";
						}
					}else{
						if($s[0] == $v['value']){
							$html .= "<input type='checkbox' name='{$v["name"]}[]' value='{$value[0]}' checked='checked'/>{$s[1]}";
						}else{
							$html .= "<input type='checkbox' name='{$v["name"]}[]' value='{$value[0]}'/>{$s[1]}";
						}
					}
				}
				$html .= "</td>";
				break;
			case 'textarea':
				$html .= "<td class='input'><textarea name='{$v["name"]}'>{$v["value"]}</textarea></td>";
				break;
			default:
				$html .= '';
		}
		$v['html'] = $html;
		return $v;
	}
	
	/**
	 *  将数组中值需要重新转成数组，转成数组 ,|
	 * @param Array $arr
	 * @return Array
	 */
	function fromDbToArr($arr){
		foreach($arr as $k=>$v){
			if(strpos($v['value'],',') && strpos($v['value'],'|')){
				$s = explode(',', $v['value']);
				foreach($s as $m=>$n){
					$ss = explode('|', $n);
					$file_conf[$ss[0]] = $ss[1];
				}
				$arr[$k]['value'] = $file_conf;
			}
		}
		return $arr;
	}
	/**
	 * 将二维数组组成一维数组
	 * @param 要处理的二维数组  Array $arr
	 * @param 要保留的键值 Array|string $field
	 * @return Array
	 */
	function arr2toArr1($arr,$field){
		$result = array();
		if(is_array($field)){
			
		}else if(!is_array($field)){
			foreach($arr as $k=>$v){
				foreach($v as $key=>$value){
					if($key == $field){
						$result[] = $value;
					}
				}
			}
		}
		return $result;
	}
	/**
	 * 专门用来处理 文章属性的函数
	 * @param unknown_type $arr
	 * @param unknown_type $field
	 * @return Ambigous <multitype:, unknown>
	 */
	function news_attr($arr,$field){
		$result = array();
		if(is_array($field)){
			
		}else{
			foreach($arr as $k=>$v){
				foreach($v as $key=>$value){
					if($key == $field){
						$result[$v['id']][] = $value;
					}
				}
			}
		}
		return $result;
	}
	/**
	 * 用来处理  二维数组中想要删除其中一个子数组中某个键位的值等于$value
	 * @param Array $arr2
	 * @param String $str
	 * @param String|number $value
	 * @return Array
	 */
	function delStrValue($arr2,$str,$value){
		$result = array();
		foreach($arr2 as $k=>$v){
			if($v[$str] != $value){
				$result[] = $arr2[$k];
			}
		}
		return $result;
	}
	/**
	 * 将存入数据库的类似于  1|男,2|女   重新转成数组
	 * @param String $string
	 * @return void|Array
	 */
	function strToArr($string){
		if(!(strpos($string, ',') && strpos($string, '|'))){
			echo '传值不符合标准';
			return;
		}else{
			$arr = explode(',', $string);
			foreach($arr as $k=>$v){
				$arr1 = explode('|', $v);
				$result[$arr1[0]] = $arr1[1];
			}
		}
		return $result;
	}
	/**
     * 组成多维数组
     * @param Array $arr 要重组的数组
     * @param Number $start  开始的层级
     * @return Array
     */
	function channleToArr($arr,$start=0){
		$id = 'catid';
		$pid = 'pid';
		$t = array();
		foreach($arr as $v){
			if($v[$pid] == $start){
				$v['data'] = channleToArr($arr,$v[$id]);
				$t[] = $v;
			}
		}
		return $t;
	}
	/**
	 * 判断一个栏目是否有子栏目
	 * @param Array $argument
	 * @return boolean  有子栏目返回真，没有返回假
	 */
	function checkCatChild($argument){
		foreach($argument as $k=>$v){
			if(empty($v['data']) && is_array($v["data"])){
				return false; //没有子栏目
			}else{
				return true; //有子栏目
			}
		}
	}
	/**
	 * 最大支持5级的栏目
	 * @param unknown_type $category
	 * @param unknown_type $css
	 * @return string
	 */
	function createCatList($category,$css){
		$php = "";
		$php .= "<ul>";
		foreach($category as $k=>$v){
			$php .= "<li class=\"".$css.'_li_'.$v["level"]."\">";
			$php .= "<a href=\"".__WEB__."/index/index/catlist/mid/".$v['mid']."/catid/".$v['catid']."\" class=\"cat_a_".$v["level"]."\">".$v["cname"]."</a>";
			if(is_array($v["data"]) && !empty($v["data"])){
				$php .= "<ul class='children1'>";
				foreach($v["data"] as $k=>$v){
					$php .= "<li class=\"".$css.'_li_'.$v["level"]."\">";
					$php .= "<a href=\"".__WEB__."/index/index/catlist/mid/".$v['mid']."/catid/".$v['catid']."\" class=\"cat_a_".$v["level"]."\">".$v["cname"]."</a>";
					if(is_array($v["data"]) && !empty($v["data"])){
						$php .= "<ul class='children2'>";
						foreach($v["data"] as $k=>$v){
							$php .= "<li class=\"".$css.'_li_'.$v["level"]."\">";
							$php .= "<a href=\"".__WEB__."/index/index/catlist/mid/".$v['mid']."/catid/".$v['catid']."\" class=\"cat_a_".$v["level"]."\">".$v["cname"]."</a>";
							if(is_array($v["data"]) && !empty($v["data"])){
								$php .= "<ul class='children3'>";
								foreach($v["data"] as $k=>$v){
									$php .= "<li class=\"".$css.'_li_'.$v["level"]."\">";
									$php .= "<a href=\"".__WEB__."/index/index/catlist/mid/".$v['mid']."/catid/".$v['catid']."\" class=\"cat_a_".$v["level"]."\">".$v["cname"]."</a>";
									if(is_array($v["data"]) && !empty($v["data"])){
										$php .= "<ul class='children4'>";
										foreach($v["data"] as $k=>$v){
											$php .= "<li class=\"".$css.'_li_'.$v["level"]."\">";
											$php .= "<a href=\"".__WEB__."/index/index/catlist/mid/".$v['mid']."/catid/".$v['catid']."\" class=\"cat_a_".$v["level"]."\">".$v["cname"]."</a>";
											$php .= "</li>";
										}
										$php .= "</ul>";
									}
									$php .= "</li>";
								}
								$php .= "</ul>";
							}
							$php .= "</li>";
						}
						$php .= "</ul>";
					}
					$php .= "</li>";
				}
				$php .= "</ul>";
			}
			$php .= "</li>";
		}
		$php .= "</ul>";
		return $php;
	}
	/**
	 * 获得栏目的层级关系 ()
	 * @param 栏目数组 Array $cats
	 * @param number $pid  //当前栏目的pid
	 * @return Ambigous <multitype:, unknown>
	 */
	function ArrToCat($cats,$catid){
		static $arr = array();
		foreach($cats as $k=>$v){
			if($v['catid'] == $catid){
				$arr[$v['catid']] = $v;
				ArrToCat($cats,$v['pid']);
			}
		}
		return $arr;
	}
	function catArr2html($cats){
		$html = '';
		if(!empty($cats[0]["data"])){
			$html .= "><a href='".__WEB__."/index/index/catlist/mid/".$cats[0]["mid"]."/catid/".$cats[0]["catid"]."'>".$cats[0]["cname"]."</a>";
			if(!empty($cats[0]["data"][0]["data"])){
				$html .= "><a href='".__WEB__."/index/index/catlist/mid/".$cats[0]["data"][0]["mid"]."/catid/".$cats[0]["data"][0]["catid"]."'>".$cats[0]["data"][0]["cname"]."</a>";
				if(!empty($cats[0]["data"][0]["data"][0]["data"])){
					$html .= "><a href='".__WEB__."/index/index/catlist/mid/".$cats[0]["data"][0]["data"][0]["mid"]."/catid/".$cats[0]["data"][0]["data"][0]["catid"]."'>".$cats[0]["data"][0]["data"][0]["cname"]."</a>";
					if(!empty($cats[0]["data"][0]["data"][0]["data"][0]["data"])){
						$html .= "><a href='".__WEB__."/index/index/catlist/mid/".$cats[0]["data"][0]["data"][0]["data"][0]["mid"]."/catid/".$cats[0]["data"][0]["data"][0]["data"][0]["catid"]."'>".$cats[0]["data"][0]["data"][0]["data"][0]["cname"]."</a>";
						if(!empty($cats[0]["data"][0]["data"][0]["data"][0]["data"][0]["data"])){
							$html .= "><a href='".__WEB__."/index/index/catlist/mid/".$cats[0]["data"][0]["data"][0]["data"][0]["data"][0]["mid"]."/catid/".$cats[0]["data"][0]["data"][0]["data"][0]["data"][0]["catid"]."'>".$cats[0]["data"][0]["data"][0]["data"][0]["data"][0]["cname"]."</a>";
							if(!empty($cats[0]["data"][0]["data"][0]["data"][0]["data"][0]["data"][0]["data"])){
								$html .= "><a href='".__WEB__."/index/index/catlist/mid/".$cats[0]["data"][0]["data"][0]["data"][0]["data"][0]["data"][0]["mid"]."/catid/".$cats[0]["data"][0]["data"][0]["data"][0]["data"][0]["data"][0]["catid"]."'>".$cats[0]["data"][0]["data"][0]["data"][0]["data"][0]["data"][0]["cname"]."</a>";
							}
						}
					}
				}
			}
		}
		return $html;
	}
	
	
	
	
	
	
	
	
	
	
	
?>